package com.corticon.dialogs.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.validator.Email;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotEmpty;


/**
 *
 * TODO: Add disable note field.
 * 
 * @author Yanglifan
 */
@Entity
@Table(name = "qs_user")
public class User extends AuditableEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    private String username;
    private String firstname;
    private String lastname;
    private String email;
    private String password;
    private String confirmPassword;
    private boolean enabled = true;
    private Set<String> permissions = new HashSet<String>();
    private UserGroup userGroup;
    private String disabledBy;
    private Date disabledOn;
    private String disabledNote;
    // Permissions Info is used to store the permissions info for the display.
    private String permissionsInfo;

    public User() {
    }

    public User(String username) {
        this.username = username;
    }

    @Length(min = 4, max = 12, message = "validate.user.username.invalid")
    @Id
    @Column(name = "userid",length = 30)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @NotEmpty(message = "validate.user.firstname.notempty")
    @Column(name = "firstname", nullable = false)
    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    @NotEmpty(message = "validate.user.lastname.notempty")
    @Column(name = "lastname", nullable = false)
    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    @NotEmpty(message = "validate.user.email.notempty")
    @Email(message = "validate.user.email")
    @Column(name = "email", nullable = false)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Length(min = 6, max = 12, message = "validate.user.password.invalid")
    @Column(name = "password", length = 30, nullable = false)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Transient
    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    @Column(name = "enabled", nullable = false)
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @CollectionOfElements(targetElement = String.class)
    @JoinTable(name = "qs_userpermission", joinColumns = @JoinColumn(name = "userassoc_userid_oid"))
    @Column(name = "permission")
    public Set<String> getPermissions() {
        if (permissions == null) {
            permissions = new HashSet<String>();
        }
        return permissions;
    }

    public void setPermissions(Set<String> permissions) {
        this.permissions = permissions;
    }

    @ManyToOne
    @JoinColumn(name = "belongstogpassoc_gpid_oid", nullable = false)
    public UserGroup getUserGroup() {
        return userGroup;
    }

    public void setUserGroup(UserGroup userGroup) {
        this.userGroup = userGroup;
    }

    public String getDisabledBy() {
        return disabledBy;
    }

    public void setDisabledBy(String disabledBy) {
        this.disabledBy = disabledBy;
    }

    @Length(max = 255, message = "user.disabledNote.length")
    public String getDisabledNote() {
        return disabledNote;
    }

    public void setDisabledNote(String disabledNote) {
        this.disabledNote = disabledNote;
    }

    @Temporal(value = TemporalType.DATE)
    public Date getDisabledOn() {
        return disabledOn;
    }

    public void setDisabledOn(Date disabledOn) {
        this.disabledOn = disabledOn;
    }

    @Transient
    public String getPermissionsInfo() {
        return permissionsInfo;
    }

    public void setPermissionsInfo(String permissionsInfo) {
        this.permissionsInfo = permissionsInfo;
    }

    @Override
    public String toString() {
        return "User ('" + getUsername() + "')";
    }
}
